/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */

#include "ScriptComponent.h"
#include "GameObject.h"

namespace PQEngine
{
	ScriptComponentType getScriptExtType(std::string type)
	{
		if(type=="cs"||type=="CS"){
			return SCRIPT_TYPE_CSHARP;
		}

		return SCRIPT_TYPE_UNKNOWN;
	}

	ScriptComponent* ScriptComponent::get(int id)
	{
		Object* obj=Object::getObject(id);
		if(obj==NULL){
			return NULL;
		}
		ObjectType type=obj->getObjectType();
		if(type==OBJECT_TYPE_COMPONENT_SCRIPT){
			return (ScriptComponent*)obj;
		}else{
			return NULL;
		}
	}
	ScriptComponent::ScriptComponent(std::string scriptPath)
		: Component(OBJECT_TYPE_COMPONENT_SCRIPT)
	{
		_scriptPath=scriptPath;

		int pos=_scriptPath.find_last_of("/");
		int pos1=_scriptPath.find_last_of(".");
		int len=pos1-pos;
		std::string pathFolder=_scriptPath.substr(0, pos+1);
		_scriptName=_scriptPath.substr(pos+1,len);
		std::string ext=_scriptPath.substr(pos1+1);
		_scriptType=getScriptExtType(ext);
	}

	ScriptComponentType ScriptComponent::getScriptType()
	{
		return _scriptType;
	}

	void ScriptComponent::attach(GameObject *obj)
	{
		assert(false);
		ScriptComponent* sc= obj->getScriptComponent(/*_scriptPath*/);//a specific script can attach to manay objects but once for single object
		if(!sc){
			//Find none 
			Component::attach(obj);
		}
	}

	void ScriptComponent::update(float deltaT)
	{
	}

	std::string ScriptComponent::getScriptName()
	{
		return _scriptName;
	}

	std::string ScriptComponent::getScriptPath()
	{
		return _scriptPath;
	}

}
